<?php
/**
 * Created by PhpStorm.
 * User: 123
 * Date: 2020/7/2
 * Time: 10:46
 */

namespace app\admin\controller;


use app\service\ResourceService;
use think\Controller;
use think\facade\Request;
use think\facade\Session;

class Common extends Controller
{
    protected $data_post;
    protected $data_get;
    protected $data_request;
    /**
     * 构造方法
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月2日10:47:45
     */
    public function __construct()
    {
        parent::__construct();
        $this->data_post = input('post.');
        $this->data_get = input('get.');
        $this->data_request = input();

        // 登录校验
        $this->IsLogin();
        // 视图初始化
        $this->ViewInit();
    }


    protected function IsLogin()
    {

//        return true;
        $uid = Session('uid');
        if(empty($uid)) {
            $this->redirect("/admin/login/index.html");
        } else {

            $this->assign('account', Session('account'));
            return true;
        }
    }
    /**
     * 上传文件方法
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月2日10:47:45
     */
    public function Doupload(){

             $filename=Request::param('field');
             $mark=Request::param('mark');
             if(empty($mark)) $mark='img';
             $oldsrc=Request::param('oldSrc');
             $src=ResourceService::uploadFile($mark,$oldsrc,$filename);
              return json($src);
    }

    public function wangeditor()
    {
        $files = request()->file();
        $imags = [];
        $errors = [];
        foreach($files as $file){
            $info = $file->move(ROOT_PATH . 'upload');
            if($info){
                $path = $info->getSaveName();
                array_push($imags,$path);
            }else{
                array_push($errors,$file->getError());
            }
        }

        if(!$errors){
            $msg['errno'] = 0;
            $msg['data'] = $imags;
            return json($msg);
        }else{
            $msg['errno'] = 1;
            $msg['data'] = $imags;
            $msg['msg'] = "上传出错";
            return json($msg);
        }
    }

    /**
     * 处理多余图片
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月2日10:47:45
     */
    public function DelRemainder($in_use,$in_all){

        if(strlen($in_use[0])<1)
        {
            unset($in_use[0]);
        }
        if(strlen($in_all[0])<1)
        {
            unset($in_all[0]);
        }

        foreach ($in_all as $k=>$v){

            if(in_array($v,$in_use))
            {
                unset($in_all[$k]);
            }
        }

        if(count($in_all)>0)
        {
            foreach ($in_all as $v){
                ## 从缓存中删除
                ResourceService::delCacheItem($v)  ;
                ## 从路径中删除
                ResourceService::delFile($v);
            }
            return DataReturn('删除成功',0);
        }else{
            //throw new \BaseException(array("code"=>403,"msg"=>"数据格式错误","errorcode"=>00000,"status"=>false));
            return DataReturn('未发现多余图片',0);
        }

    }




    /**
     * @content 删除session无用图片
     */
   public function clearSession()
    {
        ResourceService::delUploadCache();
    }

    /**
     * [ViewInit 视图初始化]
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月2日10:47:45
     */
    public function ViewInit()
    {
        // 当前操作名称
        $module_name = strtolower(request()->module());
        $controller_name = strtolower(request()->controller());
        $action_name = strtolower(request()->action());

        // 控制器静态文件状态css,js
        $module_css = $module_name.DS.'css'.DS.$controller_name;
        $module_css .= file_exists(ROOT_PATH.'static'.DS.$module_css.'.'.$action_name.'.css') ? '.'.$action_name.'.css' : '.css';
        $this->assign('module_css', file_exists(ROOT_PATH.'static'.DS.$module_css) ? $module_css : '');

        $module_js = $module_name.DS.'js'.DS.$controller_name;
        $module_js .= file_exists(ROOT_PATH.'static'.DS.$module_js.'.'.$action_name.'.js') ? '.'.$action_name.'.js' : '.js';
        $this->assign('module_js', file_exists(ROOT_PATH.'static'.DS.$module_js) ? $module_js : '');

    }
}